import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { createTransport, Transporter } from 'nodemailer';
import { emailConfig } from 'src/config';

@Injectable()
export class EmailService {

  constructor(private readonly configService: ConfigService) { }

  async sendMail({ to, subject, html }) {
    const econfig=emailConfig.useFactory(this.configService)
    const transporter: Transporter = createTransport(econfig);
    
    await transporter.sendMail({
      from: {
        name: 'coldpants系统',
        address: econfig.auth.user
      },
      to,
      subject,
      html
    }, (error, info) => {
      // if (error) {
      //   console.error('发送邮件失败:', error);
      //   throw new Error('发送邮件失败');
      // } else {
      //   console.log('邮件发送成功:', info.response);
      // }
    })
  }
}